package com.apes.fn.fico.reimbursement.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Branch;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.model.DutyCenter;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lishikun on 2021/1/29 19:19
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = DebitRepaymentItem.CONST_TABLE_NAME)
public class DebitRepaymentItem extends BaseModel {
    public static final String CONST_TABLE_NAME = "fn_debit_repayment_item";
    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "借支还款单明细", padding = 8)
    @Comment("借支还款明细编码")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="expense_debit_id", referencedColumnName="id")
    @Comment("借支单")
    private ExpenseDebit expenseDebit;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="debit_repayment_id", referencedColumnName="id")
    @Comment("还款单")
    private DebitRepayment debitRepayment;

    @Comment("借支金额")
    @Column(name = "debit_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double debitAmount = 0;

    @Comment("还款金额")
    @Column(name = "amount_repayment", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double amountRepayment = 0;

    @Comment("剩余金额")
    @Column(name = "balance", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double balance = 0;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "pay_method", referencedColumnName = "id")
    @Comment("支付方式")
    private Enumeration payMethod;

    @Comment("支付人")
    @Column(name = "pay_people", length = 120)
    private String payPeople;

    @JoinColumn(name = "account_bank_name")
    @Comment("收款账户名称")
    private String accountBankName;

    @Comment("账户")
    @Column(name = "account_bank_number", length = 120)
    private String accountBankNumber;


    @Comment("开户行")
    @Column(name = "account_bank")
    private String accountBank;

    @Comment("支行")
    @Column(name = "subbranch", length = 200)
    private String subbranch;

    @Comment("备注")
    @Column(name = "remark", length = 120)
    private String remark;

    @OneToMany(mappedBy = "debitRepaymentItem", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<RepaymentInvoiceItem> repaymentInvoiceItems = new ArrayList<>();

    public List<RepaymentInvoiceItem> getRepaymentInvoiceItems() {
        for (RepaymentInvoiceItem repaymentInvoiceItem : repaymentInvoiceItems) {
            repaymentInvoiceItem.setDebitRepaymentItem(this);
        }
        return repaymentInvoiceItems;
    }

    public void setRepaymentInvoiceItems(List<RepaymentInvoiceItem> repaymentInvoiceItems) {
        if (repaymentInvoiceItems==null) repaymentInvoiceItems = new ArrayList<>();
        this.repaymentInvoiceItems.clear();
        this.repaymentInvoiceItems = repaymentInvoiceItems;
    }


    @Transient
    private PartyRole borrower;

    @Transient
    private DutyCenter dutyCenter;

    public PartyRole getBorrower() {
        if (expenseDebit == null) return null;
        return expenseDebit.getBorrower();
    }
//
//    public DutyCenter getDutyCenter() {
//        if (expenseDebit == null) return null;
//        return expenseDebit.getDutyCenter();
//    }


}
